<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/8/16
 * Time: 17:40
 * 订单
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\BuyRecord;
use think\Db;

class Buyorder extends Api{
	protected $noNeedRight = ['*'];
	
	/**
	 * @param type 结算类型 goods单商品 cart购物车
	 * @param record_id 单商品结算时的期数ID
	 * @param number 单商品结算的数量
	 * @param cart_ids 购物车结算时的购物车ID 以,号拼接
	 */
	public function confirmOrder(){
		
		$model = new BuyRecord();
		$res = $model->createOrder($this->auth->id);
		if($res) {
			$this->success(__('查询成功'), $res);
		} else {
			$this->error($model->getError());
		}
		
	}
	
	/**
	 * 创建订单
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public function createOrder(){
		$model = new BuyRecord();
		$res = $model->createOrder($this->auth->id, 2);
		if($res === true) {
			$this->success(__('支付成功'));
		} elseif($res == -1){
			$this->error($model->getError(), ['status' => 0]);
		} else {
			$this->error($model->getError());
		}
	}
	
	public function index(){
		$page = input('post.page', 1, 'int');
		
		$rows = Db::name('buyRecord')->alias('b')
			->join('goodsRecord r', 'b.goods_record_id = r.id', 'left')
			->join('goods g', 'r.goods_id = g.id', 'left')
			->where(['b.user_id' => $this->auth->id])
			->order(['b.create_time' => 'DESC'])
			->page($page)
			->field('b.id, b.goods_record_id, b.number, b.status, from_unixtime(b.create_time) as time, g.image, g.goods_name')
			->select();
		
		if($rows) {
			$statusList = BuyRecord::$statusList;
			foreach($rows as $k => $v) {
				$rows[$k]['status_text'] = __(isset($statusList[$v['status']])?$statusList[$v['status']]:'');
				
			}
		}
		$this->success(__('查询成功'), $rows);
	}
	
	public function detail(){
	
	}
	
	
}
